package com.bls.blslib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bls.blslib.R;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/17.
 */

public class TitleView extends RelativeLayout {

    private TitleView titleView;
    public ImageView titleLeftIv1;
    public ImageView titleLeftIv2;
    public TextView titleMiddleText;
    public ImageView titleRightIv;
    public TextView titleRightTv;
    public RelativeLayout titleRoot;
    public View titleBottomLine;
    private OnTitleClickEvent onTitleClickEvent;

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        titleView = this;
        LayoutInflater.from(context).inflate(R.layout.view_title_bar, this, true);
        titleRoot = findViewById(R.id.title_root);
        titleLeftIv1 = findViewById(R.id.title_left_iv_1);
        titleLeftIv2 = findViewById(R.id.title_left_iv_2);
        titleMiddleText = findViewById(R.id.title_middle_text);
        titleRightIv = findViewById(R.id.title_right_iv);
        titleRightTv = findViewById(R.id.title_right_tv);
        titleBottomLine = findViewById(R.id.title_bottom_line);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        if (null != attributes) {
            boolean titleLeftIv1Visible = attributes.getBoolean(R.styleable.TitleView_title_left_iv_1_visible, false);
            if (titleLeftIv1Visible) titleLeftIv1.setVisibility(VISIBLE);
            else titleLeftIv1.setVisibility(GONE);
            attributes.recycle();
        }

        titleLeftIv1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onTitleClickEvent)
                    onTitleClickEvent.onTitleClick(TitleClickEnum.Left_1);
            }
        });
        titleLeftIv2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onTitleClickEvent)
                    onTitleClickEvent.onTitleClick(TitleClickEnum.Left_2);
            }
        });
        titleRightIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onTitleClickEvent)
                    onTitleClickEvent.onTitleClick(TitleClickEnum.Right_1);
            }
        });
        titleRightTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onTitleClickEvent)
                    onTitleClickEvent.onTitleClick(TitleClickEnum.Right_2);
            }
        });
    }

    /**
     * 设置Title 背景
     *
     * @param resid 资源
     */
    public TitleView setTitleViewBg(int resid) {
        titleRoot.setBackgroundColor(resid);
        return titleView;
    }

    public TitleView setTitleElevation(float z) {
        titleRoot.setTranslationZ(z);
        return titleView;
    }

    /**
     * 设置显示的按钮  默认都不显示
     */
    public TitleView setBtnClickMode(int left, int right) {
        if (left == 1) {
            titleLeftIv1.setVisibility(VISIBLE);
        } else if (left == 2) {
            titleLeftIv2.setVisibility(VISIBLE);
        }
        if (right == 1) {
            titleRightIv.setVisibility(VISIBLE);
        } else if (right == 2) {
            titleRightTv.setVisibility(VISIBLE);
        }
        return titleView;
    }

    public TitleView setTitleMiddleText(String strTitle) {
        titleMiddleText.setText(strTitle);
        return titleView;
    }

    public TitleView setTitleRightText(String strTitle) {
        titleRightTv.setText(strTitle);
        return titleView;
    }


    public void setTitleClickListener(OnTitleClickEvent onTitleClickEvent) {
        this.onTitleClickEvent = onTitleClickEvent;
    }



    public interface OnTitleClickEvent {
        void onTitleClick(TitleClickEnum titleClickEnum);
    }

    public enum TitleClickEnum {
        Left_1, Left_2, Right_1, Right_2
    }
}
